const Koa = require('koa');
const fs = require('fs');
const path = require('path');
;
const bodyParser = require('koa-bodyparser');

const app = new Koa();

app.use(bodyParser({
  formLimit: '50mb',
}));

app.use(async (ctx, next) => {

  if(ctx.method === 'GET') {
    await dealGetRequest(ctx); 
  }
  if(ctx.request.method === 'POST' ) {
    await dealPostRequest(ctx)
  } 

  // if(ctx.request.url === '/update') {
  //   ctx.set({
  //       'Content-Type':'text/event-stream',
  //       'Cache-Control':'no-cache',
  //       'Connection': 'keep-alive'
  //   });
  //   ctx.body = stream;
  // }
  await next();
});


/** 处理get请求 */
async function dealGetRequest(ctx) {
  // 入口请求
  if(ctx.request.url === '/' || ctx.request.url === '/index' ) {
    ctx.body = fs.readFileSync(path.join(process.cwd(), 'index.html'), {
      encoding: 'utf-8'
    });
    return;
  }
  // 简单处理静态资源
  if(ctx.request.url.indexOf('.') !== -1) {
    ctx.body = fs.readFileSync(path.join(process.cwd(), ctx.request.url), {
      encoding: 'utf-8'
    });
    return;
  }
}
/** 处理post请求 */
async function dealPostRequest(ctx) {

}



app.listen(3000);